﻿using System;
using System.Linq;
using System.Collections.Generic;
namespace ToDoList.Logic
{
    public class InventoryLogic
    {
        /// <summary>
        /// 创建清单
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool CreateInventory(string listName,int userId)
        {
            var userList = Initialize.UserList.SingleOrDefault(N=>N.UserId== userId);
            //userList.UserInventoryList = new List<Inventory>();
            if (userList != null)
            {
                var inventory = new Inventory
                {
                    CreatTime = DateTime.Now.ToLocalTime().ToString(),
                    ListName = listName,
                    InventoryId= Initialize.InventoryList.Max(inventory1 => inventory1.InventoryId) + 1,
                    CreatName = userList.Name,
                };                
                userList.UserInventoryList.Add(inventory);
                Initialize.InventoryList.Add(inventory);
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 添加事项到清单
        /// </summary>
        /// <param name="ListId"></param>
        /// <param name="Content"></param>
        /// <param name="Mark"></param>
        /// <param name="TimeOfExpiration"></param>
        /// <returns></returns>
        public bool AddItem(int inventoryId, string content,string mark,DateTime timeOfExpiration)
        {
            var inventory = Initialize.InventoryList.SingleOrDefault(props => props.InventoryId == inventoryId);
            if (inventory != null)
            {
                Item item = new Item
                {
                    Content = content,
                    CreatTime = DateTime.Now.ToLocalTime().ToString(),
                    Mark = mark,
                    ItemId= Initialize.ItemList.Max(item1 => item1.ItemId) + 1,
                    State = false,
                    TimeOfExpiration = timeOfExpiration,
                };               
                inventory.ItemListOfInventory.Add(item);
                inventory.User.UserItemList.Add(item);
                Initialize.ItemList.Add(item);
                return true;
            }
            else
                return false;             
        }

        /// <summary>
        /// 删除事项
        /// </summary>
        /// <param name="ItemId"></param>
        /// <param name="ListId"></param>
        /// <returns></returns>
        public bool DeleteItem(int itemId, int inventoryId)
        {
            var inventory = Initialize.InventoryList.SingleOrDefault(I => I.InventoryId == inventoryId);
            var item = Initialize.ItemList.SingleOrDefault(I => I.ItemId == itemId);
            if (inventory!=null&& item != null)
            {
                inventory.User.UserItemList.Remove(item);
                inventory.ItemListOfInventory.Remove(item);
                Initialize.ItemList.Remove(item);
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 修改清单名字
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="newListName"></param>
        /// <returns></returns>
        public bool Modification(int userId,int inventoryId, string newListName)
        {
            var user = Initialize.UserList.SingleOrDefault(N => N.UserId==userId);
            var inventory = Initialize.InventoryList.SingleOrDefault(S => S.InventoryId == inventoryId);
            if (user != null && inventory != null)
            {
                inventory.ListName = newListName;
                return true;
            }
            else
                return false;
        }
    }
}
